package org.mineground.commands.irc;

import java.util.Timer;
import java.util.TimerTask;
import org.bukkit.plugin.Plugin;
import org.jibble.pircbot.Colors;
import org.jibble.pircbot.User;
import org.mineground.Main;
import org.mineground.handlers.irc.CommandExecutor;
import org.mineground.handlers.irc.UserLevel;

/**
 *
 * @file ReloadPlugin.java (2012)
 * @author Daniel Koenen
 * 
 */
public class HandlePlugin implements CommandExecutor {
    @Override
    public void onCommand(User sender, UserLevel level, String channel, String command, String args[]) {
	if (level.compareTo(UserLevel.IRC_SOP) < 0) {
	    return;
        }

	if (args.length < 2) {
	    Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Usage:" + Colors.NORMAL + " !plugin [reload/unload/load] [plugin name]");
	    return;
	}
        
        String pluginCommand = args[0];
        String pluginName = args[1];
        
        if (pluginName.equalsIgnoreCase("LVM")) {
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Error: It's not safe to reload that plugin right now, sorry.");
            return;
        }

        Plugin pluginToReload = Main.getInstance().getServer().getPluginManager().getPlugin(pluginName);
        
        if (pluginCommand.equalsIgnoreCase("reload")) {
            if (pluginToReload == null) {
                Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Error: Couldn't find that plugin.");
                return;
            }
            
            Main.getInstance().getPluginLoader().disablePlugin(pluginToReload);
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.DARK_BLUE + pluginName + " has been disabled, it'll be reloaded in 3 seconds...");
            new Timer().schedule(new PluginReloadTask(channel, pluginToReload), 3000);
        }
        
        else if (pluginCommand.equalsIgnoreCase("load")) {
            Main.getInstance().getPluginLoader().enablePlugin(pluginToReload);
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.DARK_BLUE + pluginName + " has been enabled.");
        }
        
        else if (pluginCommand.equalsIgnoreCase("unload")) {
            Main.getInstance().getPluginLoader().disablePlugin(pluginToReload);
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.DARK_BLUE + pluginName + " has been disabled.");
        }
        
        else {
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Usage:" + Colors.NORMAL + " !plugin [reload/unload/load] [plugin name]");
	    return;
	}
    }
    
    private class PluginReloadTask extends TimerTask {
        private String channelName;
        private Plugin pluginToReload;
        
        public PluginReloadTask(String channelName, Plugin pluginToReload) {
            this.channelName = channelName;
            this.pluginToReload = pluginToReload;
        }
        
        @Override
        public void run() {
            Main.getInstance().getPluginLoader().enablePlugin(pluginToReload);
            Main.getInstance().getIRCHandler().sendMessage(channelName, Colors.DARK_BLUE + "Plugin has been reloaded!");
            cancel();
        }
    }
}
